package org.flower.dao.impl;

import org.flower.dao.AdminUserDao;
import org.flower.entity.AdminUser;
import org.flower.utils.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Aizen
 */
public class AdminUserDaoImpl extends BaseDao implements AdminUserDao {
    @Override
    public List<Integer> getZhouAdminUser() {
        List<Integer> list = new ArrayList<>();
        String sql = "SELECT COUNT(1) c FROM `adminuser` WHERE TO_DAYS(`addDate`) = TO_DAYS(SUBDATE(NOW(), ?))";
        for (int i = 0; i <= 7; i++) {
            ResultSet rs = executeQuery(sql, i);
            try {
                if(rs.next()){
                    list.add(rs.getInt("c"));
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                try {
                    closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return list;
    }

    @Override
    public AdminUser getAdminUser(String uName, String uPwd) {
        AdminUser adminUser = null;
        String sql = "SELECT * FROM adminuser WHERE uName = ? AND upwd = ?";
        ResultSet rs = executeQuery(sql,uName,uPwd);
        try {
            if(rs.next()){
                adminUser = new AdminUser(rs.getInt("id"),rs.getString("uName"),rs.getString("uPwd"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return adminUser;
    }

    @Override
    public int getDayAddAdminUser() {
        int result = -1;
        String sql = "SELECT COUNT(1) c FROM adminuser WHERE TO_DAYS(`addDate`) = TO_DAYS(NOW())";
        ResultSet rs = executeQuery(sql);
        try {
            if (rs.next()){
                result = rs.getInt("c");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public int getAllAdminUser() {
        int result = -1;
        String sql = "SELECT COUNT(1) c FROM adminuser";
        ResultSet rs = executeQuery(sql);
        try {
            if(rs.next()){
                result = rs.getInt("c");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return result;
    }
}
